import React, { Component } from 'react'
// import IceContainer from '@icedesign/container';
import DataBinder from '@icedesign/data-binder';
import { Loading, Table } from '@icedesign/base';
import { GetUrlParam, LoginReq } from '../../../baseConfig';
import './AbnormalTable.scss';

@DataBinder({
  TableBoxData: {
    url: `${global.constants.GOLDEYEGETHOST}cattle/index_info`,
    type: 'get',
    params: {},
    defaultBindingData: {}
  },
})

/**
 * 市值异动提示
 */
export default class AbnormalTable extends Component {

  static propTypes = {
    // secu_code: PropTypes.string.isRequired,
    // type: PropTypes.string,
  };
  
  constructor(props) {
    super(props);
    this.state = {
      newData:[],
      capital_transaction:[],
      capital_data:[],
      sum:{}
    }
  }
  
  componentDidMount() {
    this.props.updateBindingData('TableBoxData',{
      params: {
        secu_code:GetUrlParam("secu_code"),
      }
    }, (data) => {
      // console.log('获取到tab的data',data.data.capital_transaction)
      if(data && data.flag){
        // console.log("data",data.data)
        this.setState({
          capital_transaction:data.data.capital_transaction
        });
        this.pushArray(this.state.capital_transaction);
      }else{
        this.setState({
          message: true
        })
      }
    });
  }

  getCellProps = (rowIndex,colIndex) => {
    // let sum = {}
    const ret = {
      colSpan: 1,
      rowSpan: this.state.sum[`'${rowIndex}'`] && colIndex === 0 ? this.state.sum[`'${rowIndex}'`] : 1
    }
    return ret
  }

  pushArray = (data) => {
    // let num = 0;
    let datas = [];
    let sum = {};
    let currentKey;
    for(let i=0;i<data.length;i++){
      for(let j=0;j<data[i].value_list.length;j++){
        if (j === 0){
          currentKey = `'${datas.length}'`
          sum[currentKey] = 1
        } else {
          sum[currentKey] ++
        }
        datas.push({index_type:`${data[i].index_type}`,index_name:`${data[i].value_list[j].index_name}`,index_value:`${data[i].value_list[j].index_value}`})
        // datas.push({index_type:`${data[i].index_type}`,value_list:{index_name:`${data[i].value_list[j].index_name}`}})
      }
    }

    this.setState({
      capital_data:datas,
      sum:sum
    })
    // console.log("datas",this.state.capital_data);
  }

  render() {
    return (
      <div style={{padding: '0 16px'}}>
        <Table dataSource={this.state.capital_data} getCellProps={this.getCellProps}>
          <Table.Column align="center" title="指标类型" dataIndex="index_type" />
          <Table.Column align="center" title="指标名称" dataIndex="index_name" />
          <Table.Column align="center" title="指标值"  dataIndex="index_value" />
        </Table>
      </div>
    )
  }
}

const styles = {
  textCenter:{
    textAlign:'center'
  },
  TableBox:{
    display:'inline-flex',
    width:'100%',
    // maxWidth:'1000px',
    margin:'0 auto'
  },
  tr:{
    width:'100%',
    display:'inline-flex',
  },
  th:{
    width:'33.333%',
    padding:'15px 0',
    lineHeight:'14px',
    backgroundColor:'#f1f1f1',
    textAlign:'center',
  },
  td:{
    width:'33.333%',
    textAlign:'center',
    padding:'15px 0',
    lineHeight:'14px',
    borderRight:'1px solid #f1f1f1'
  },
  tds:{
    width:'33.333%',
    textAlign:'center',
    padding:'15px 0',
    lineHeight:'14px',
    borderLeft:'1px solid #f1f1f1',
    borderRight:'1px solid #f1f1f1'
  }
}